package com.cskaoyan.service.wx;

import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.wx.search.SearchIndexVo;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.util.UserIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author LY
 * @since 2022/06/09 16:57
 */
@Service
public class WxSearchServiceImpl implements WxSearchService{
    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public SearchIndexVo index() {
        MarketKeywordExample example = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        example.setOrderByClause("is_default desc");
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(example);
        SearchIndexVo searchIndexVo = new SearchIndexVo();
        searchIndexVo.setDefaultKeyword(marketKeywords.get(0));
        ArrayList<MarketKeyword> hotKeywordList = new ArrayList<>();
        for (MarketKeyword marketKeyword : marketKeywords) {
            if(marketKeyword.getIsHot()){
                hotKeywordList.add(marketKeyword);
            }
        }
        searchIndexVo.setHotKeywordList(hotKeywordList);

        Integer userId = UserIdUtil.getUserIdFromSubject();
        MarketSearchHistoryExample historyExample = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria historyExampleCriteria = historyExample.createCriteria();
        historyExampleCriteria.andUserIdEqualTo(userId);
        historyExampleCriteria.andDeletedEqualTo(false);
        historyExample.setOrderByClause("update_time desc");
        historyExample.setDistinct(true);
        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(historyExample);
        searchIndexVo.setHistoryKeywordList(marketSearchHistories);

        return searchIndexVo;
    }

    @Override
    public List<String> helper(String keyword) {
        List<String> list = new ArrayList<>();
        if(" ".equals(keyword)){
            list.add("HIII LADIES！");
            return list;
        }
        MarketGoodsExample example = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andNameLike("%" + keyword + "%");
        criteria.andDeletedEqualTo(false);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(example);
        for (MarketGoods marketGood : marketGoods) {
            list.add(marketGood.getName());
        }
        return list;
    }

    @Transactional
    @Override
    public void clearHistoryKeyword(Integer userId) {
        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);

        MarketSearchHistory record = new MarketSearchHistory();
        record.setDeleted(true);
        record.setUpdateTime(new Date());

        marketSearchHistoryMapper.updateByExampleSelective(record, example);
    }
}
